"""
"""
import unittest as ut
import sys

import basicVdsTest as vdst
import vdsClient as vdsc
from storageConnectionTestUtils import eStorageType, connectToStorage, validateConnections, limitTestToConnectionType, CONNECTIONS_OPTION_DEFAULT
from confUtils import Validate
from testUtils import cleanVdsm
from agentUtils import connectAgent

sys.path.append("../../") # Vdsm root dir

class VerifyDevicesFilter(vdst.BasicVDSTest):
    #For convenience. I didn't put it in the base class because I didn't want to commit.
    mycfg = property(lambda self: self.cfg[self.__class__.__name__])
    @classmethod
    def getConfigTemplate(cls):
        return {cls.__name__ : {
                "VdsUri1" : {"default" : "http://server:54321"},
                "Connections" : {"default" : CONNECTIONS_OPTION_DEFAULT, "validator" : validateConnections},
                "HostID1" : {"default" : 7, "validator" : Validate.int},
            }
        }

    def setUp(self):
        #Setup clients
        self.client = vdsc.VDSClient()
        self.client.setURI(self.mycfg["VdsUri1"])
        self.agent = connectAgent(self.mycfg["VdsUri1"])

        #Make sure env is clean
        resp = self.assertVdscOK(self.client.getConnectedStoragePoolsList())
        self.assertTrue(len(resp['poollist']) == 0, "VDSM is connected to a pool. Dirty env. Bailing out.")

        #Handle storage configuration
        connectToStorage(self.client, self.mycfg["Connections"])


    def test(self):
        self.assertRaises(Exception, limitTestToConnectionType, eStorageType.nfs, self.mycfg["Connections"])
        actualDevs = self.agent.getMPDevList()
        actualDevs = [dev.split()[0] for dev in actualDevs]
        filteredDevs = self.agent.getLVMDevList(True)
        devs = [dev.split("[")[0].strip() for dev in filteredDevs[:-4]]
        (disks, partitions, lvm_whole_devs, lvm_devs) = [stat.split()[0] for stat in filteredDevs[-4:]]
        assert(len(actualDevs) == (int(disks) + int(partitions) + int(lvm_whole_devs) + int(lvm_devs)))
        for dev in actualDevs:
            dev = "/dev/mapper/" + dev.split()[0].strip()
            assert(dev in devs)


    def tearDown(self):
        cleanVdsm(self.client, self.mycfg["HostID1"], self.mycfg["Connections"])

def suite():
    tests = {
        VerifyDevicesFilter : ["test"]
    }

    resSuite = ut.TestSuite()
    for testcase, methods in tests.iteritems():
        resSuite.addTests(map(testcase, methods))

    return resSuite

validateConfig = lambda cfg : vdst.validateSuiteConfig(suite(), cfg)
